﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WorldBuilder
{
    class XMLWriter
    {
        XmlDocument xmldoc;
        XmlNode xmlnode;
        XmlElement xmlelem;
        XmlText xmltext;
  

        public XMLWriter()
        {
           
            
           
        }
        public void WriteLevelData_2D(LevelData leveldata)
        {
            /* We will have to Discern if we are writing data for a 2D level or a 3D level */


            xmldoc = new XmlDocument();
            //let's add the XML declaration section
            xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xmlnode);

            //let's add the root element
            xmlelem = xmldoc.CreateElement("", "WorldBuilder", "");

            xmldoc.AppendChild(xmlelem);

            //let's add another element (child of the root)
            CreateChildNode("LoadingInfo", "I would like to put Loading prefs here.");

            CreateChildNode("BuilderData", null);

            CreateSubChildNode("Description", leveldata.GetDescription());
            CreateSubChildNode("LevelProjection", leveldata.GetLevelProjection());
            CreateSubChildNode("MapSizeWidth", leveldata.GetMapSizeX().ToString());
            CreateSubChildNode("MapSizeHeight", leveldata.GetMapSizeY().ToString());
            CreateSubChildNode("TileSizeWidth", leveldata.GetTileSizeX().ToString());
            CreateSubChildNode("TileSizeHeight", leveldata.GetTileSizeY().ToString());
            CreateSubChildNode("TileData", null);
            /*
             * It is nice that this works but i need to implement it. 
            Create3rdLevelChildNode("Tile1", "..\\..\\Tile1.png");
            Create3rdLevelChildNode("Tile2", "..\\..\\Tile2.png");
            Create3rdLevelChildNode("Tile3", "..\\..\\Tile3.png");
            Create3rdLevelChildNode("Tile4", "..\\..\\Tile4.png");
             * */
            CreateSubChildNode("MapData", leveldata.GetMap_string());


            try
            {
                xmldoc.Save("Levels\\Sample2D.xml"); // So this will have to be an input as well. 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void WriteLeveldata_3D(LevelData leveldata)
        {
            /* We will have to Discern if we are writing data for a 2D level or a 3D level */


            xmldoc = new XmlDocument();
            //let's add the XML declaration section
            xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xmlnode);

            //let's add the root element
            xmlelem = xmldoc.CreateElement("", "WorldBuilder", "");

            xmldoc.AppendChild(xmlelem);

            //let's add another element (child of the root)
            CreateChildNode("LoadingInfo", "I would like to put Loading prefs here.");

            CreateChildNode("BuilderData", null);

            CreateSubChildNode("Description", leveldata.GetDescription());
            CreateSubChildNode("MapSizeWidth", leveldata.GetMapSizeX().ToString());
            CreateSubChildNode("MapsizeHeight", leveldata.GetMapSizeY().ToString());
            CreateSubChildNode("Vertices", leveldata.GetVertices_string());

            try
            {
                xmldoc.Save("Levels\\Sample3D.xml"); // So this will have to be an input as well. 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }
        public void WriteLevelData(LevelData leveldata)
        {
            /* We will have to Discern if we are writing data for a 2D level or a 3D level */

          
            xmldoc = new XmlDocument();
            //let's add the XML declaration section
            xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmldoc.AppendChild(xmlnode);
             
            //let's add the root element
            xmlelem = xmldoc.CreateElement("", "WorldBuilder", "");
            
            xmldoc.AppendChild(xmlelem);

            //let's add another element (child of the root)
            CreateChildNode("LoadingInfo", "I would like to put Loading prefs here.");

            CreateChildNode("BuilderData", null);

            CreateSubChildNode("Description", leveldata.GetDescription());
            CreateSubChildNode("LevelProjection", leveldata.GetLevelProjection());
            CreateSubChildNode("MapSizeWidth",leveldata.GetMapSizeX().ToString());
            CreateSubChildNode("MapSizeHeight",leveldata.GetMapSizeY().ToString());
            CreateSubChildNode("TileSizeWidth", leveldata.GetTileSizeX().ToString());
            CreateSubChildNode("TileSizeHeight", leveldata.GetTileSizeY().ToString());
            CreateSubChildNode("TileData", null);
            /*
             * It is nice that this works but i need to implement it. 
            Create3rdLevelChildNode("Tile1", "..\\..\\Tile1.png");
            Create3rdLevelChildNode("Tile2", "..\\..\\Tile2.png");
            Create3rdLevelChildNode("Tile3", "..\\..\\Tile3.png");
            Create3rdLevelChildNode("Tile4", "..\\..\\Tile4.png");
             * */
            CreateSubChildNode("MapData",leveldata.GetMap_string() );

           
            try
            {
                xmldoc.Save("Levels\\Sample2D.xml"); // So this will have to be an input as well. 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }

        void CreateChildNode(string ElementName, string Description,string prefix, string nspace)
        {
            xmlelem = xmldoc.CreateElement(prefix, ElementName, nspace);
            xmltext = xmldoc.CreateTextNode(Description);
            xmlelem.AppendChild(xmltext);       
            xmldoc.ChildNodes.Item(1).AppendChild(xmlelem);
    
        }
        void CreateChildNode(string ElementName, string Description)
        {
            xmlelem = xmldoc.CreateElement("", ElementName, "");
            xmltext = xmldoc.CreateTextNode(Description);
            xmlelem.AppendChild(xmltext);
            xmldoc.ChildNodes.Item(1).AppendChild(xmlelem);

        }

        void CreateSubChildNode(string ElementName, string Description, string prefix, string nspace)
        {
            xmlelem = xmldoc.CreateElement(prefix, ElementName, nspace);
            xmltext = xmldoc.CreateTextNode(Description);
            xmlelem.AppendChild(xmltext);
            xmldoc.ChildNodes.Item(1).ChildNodes.Item(1).AppendChild(xmlelem);
        }

        void CreateSubChildNode(string ElementName, string Description)
        {
            xmlelem = xmldoc.CreateElement("", ElementName, "");
            xmltext = xmldoc.CreateTextNode(Description);
            xmlelem.AppendChild(xmltext);
            xmldoc.ChildNodes.Item(1).ChildNodes.Item(1).AppendChild(xmlelem);
        }

        void Create3rdLevelChildNode(string ElementName, string Description, string prefix, string nspace)
        {
            xmlelem = xmldoc.CreateElement(prefix, ElementName, nspace);
            xmltext = xmldoc.CreateTextNode(Description);
            xmlelem.AppendChild(xmltext);
            xmldoc.ChildNodes.Item(1).ChildNodes.Item(1).ChildNodes.Item(8).AppendChild(xmlelem);
        }

        void Create3rdLevelChildNode(string ElementName, string Description)
        {
            xmlelem = xmldoc.CreateElement("", ElementName, "");
            xmltext = xmldoc.CreateTextNode(Description);
            xmlelem.AppendChild(xmltext);
            xmldoc.ChildNodes.Item(1).ChildNodes.Item(1).ChildNodes.Item(8).AppendChild(xmlelem);
        }
    }
}
